package com.zone.vehicle.lib.view.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Base64;
import android.util.LruCache;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

/**
 * Date:2020/5/20
 * author:wuyan
 */
public class ImageUtil {
    private LruCache<String, Bitmap> mMemoryCache;
    private int cacheSize;

    //设置图片大小
    private static RoundedCorners roundedCorners = new RoundedCorners(50);
    private static RequestOptions options = RequestOptions.bitmapTransform(roundedCorners)
            .placeholder(new ColorDrawable(Color.GRAY))//设置占位图
//                    .error(R.mipmap.logo_app)//设置异常图片
            .override(300, 300);//降低内存消耗

    /**
     * base64转为bitmap
     *
     * @param base64Data
     * @return
     */
    public static Bitmap base64ToBitmap(String base64Data) {
        byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }

    public static Bitmap getBitmap(Context context, int resId) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        TypedValue value = new TypedValue();
        context.getResources().openRawResource(resId, value);
        options.inTargetDensity = value.density;
        options.inScaled = false;//不缩放
        return BitmapFactory.decodeResource(context.getResources(), resId, options);
    }

    public static void load(Context context, ImageView imageView, String url){
        Glide.with(context)
                .asBitmap()
                .load(url)
                .apply(options)
                .into(imageView);
    }
}
